package top.superxuqc.loginbili.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Service;
import top.superxuqc.loginbili.util.MutilThreadHttpClient;
import top.superxuqc.loginbili.util.MyHttpClient;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;

@Service
public class DownVideoService {
    private MyHttpClient httpClient = new MyHttpClient();


    public String getDetail(String cookies,String av){
        return httpClient.get(cookies, "http://api.bilibili.com/x/web-interface/view?aid=" + av);


    }

    public String getHistory(HttpServletRequest request){
        return httpClient.getHistory(request);

    }
    public String getHistory(HttpServletRequest request,String cookies){
        return httpClient.getHistory(request,cookies);

    }
    /**
     * 获取推荐视频
     * @param request
     * @return
     */
    public JSONArray getLike(HttpServletRequest request){

        String history = getHistory(request);
        JSONArray historyArray = JSONObject.parseObject(history).getJSONObject("data").getJSONArray("list");

        JSONArray arrays = new JSONArray();

        for (Object his: historyArray
             ) {
            JSONObject hist = JSONObject.parseObject(his.toString());
            if(!hist.get("badge").toString().equals("")){
                continue;
            }else {
                String kid = hist.get("kid").toString();
                String currnt40 = httpClient.getLike(request, kid);
                JSONObject jsonObject = JSONObject.parseObject(currnt40);
                JSONArray datas = jsonObject.getJSONArray("data");
                JSONArray array = new JSONArray();
                for (int i = 0; i < 5; i++) {
                    array.add(datas.get(i));
                }


                arrays.add(array);
            }

        }

        return arrays;

    }

    public JSONArray getLike(HttpServletRequest request,String cookies){

        String history = getHistory(request,cookies);
        JSONArray historyArray = JSONObject.parseObject(history).getJSONObject("data").getJSONArray("list");

        JSONArray arrays = new JSONArray();

        for (Object his: historyArray
        ) {
            JSONObject hist = JSONObject.parseObject(his.toString());
            if(!hist.get("badge").toString().equals("")){
                continue;
            }else {
                String kid = hist.get("kid").toString();
                String currnt40 = httpClient.getLike(kid,cookies);
                JSONObject jsonObject = JSONObject.parseObject(currnt40);
                JSONArray datas = jsonObject.getJSONArray("data");
                JSONArray array = new JSONArray();
                for (int i = 0; i < 5; i++) {
                    array.add(datas.get(i));
                }


                arrays.add(array);
            }

        }

        return arrays;

    }
    public String getSimilar(String av,String cookies){





        String currnt40 = httpClient.getLike( av,cookies);
        JSONObject jsonObject = JSONObject.parseObject(currnt40);
        JSONArray datas = jsonObject.getJSONArray("data");
        JSONArray array = new JSONArray();
        for (int i = 0; i < 5; i++) {
            array.add(datas.get(i));
        }




        return array.toJSONString();

    }
    public String getTop(){
        JSONArray top = httpClient.getTop();
        return top.toString();
    }

    /**
     * 获取flv播放地址
     * @param av
     * @param request
     * @return
     */
    public JSONArray getFlvVideo(String av, HttpServletRequest request){
        HashMap<String,String> map = new HashMap<>();
        if(av.startsWith("BV")) {
            map.put("bvid", av);
        }else {
            map.put("aid",av);
        }
        String result = httpClient.get(request, "http://api.bilibili.com/x/web-interface/view", map);
        JSONObject videoInfo = JSONObject.parseObject(result).getJSONObject("data");

        String url = "http://api.bilibili.com/x/player/playurl";

        JSONArray urls = new JSONArray();
        int num = (int)videoInfo.get("videos");
        JSONArray pages = videoInfo.getJSONArray("pages");
        for(int i = 0 ; i < num ; i++){
            JSONObject page = pages.getJSONObject(i);
            Map<String,String> map2 = new HashMap<>();
            map2.put("avid",videoInfo.get("aid").toString());
            map2.put("cid",page.get("cid").toString());
            map2.put("qn","120");
            map2.put("fnval","0");
            String s = httpClient.get(request, url, map2);
            JSONObject resultVideo = JSONObject.parseObject(s);
            JSONArray video = resultVideo.getJSONObject("data").getJSONArray("durl");


            urls.add(video);


        }


        return urls;


    }


    public JSONArray getFlvVideo(String av, String cookies){
        HashMap<String,String> map = new HashMap<>();
        if(av.startsWith("BV")) {
            map.put("bvid", av);
        }else {
            map.put("aid",av);
        }
        String result = httpClient.get(cookies, "http://api.bilibili.com/x/web-interface/view", map);
        JSONObject videoInfo = JSONObject.parseObject(result).getJSONObject("data");

        String url = "http://api.bilibili.com/x/player/playurl";

        JSONArray urls = new JSONArray();
        int num = (int)videoInfo.get("videos");
        JSONArray pages = videoInfo.getJSONArray("pages");
        for(int i = 0 ; i < num ; i++){
            JSONObject page = pages.getJSONObject(i);
            Map<String,String> map2 = new HashMap<>();
            map2.put("avid",videoInfo.get("aid").toString());
            map2.put("cid",page.get("cid").toString());
            map2.put("qn","120");
            map2.put("fnval","0");
            String s = httpClient.get(cookies, url, map2);
            JSONObject resultVideo = JSONObject.parseObject(s);
            JSONArray video = resultVideo.getJSONObject("data").getJSONArray("durl");


            urls.add(video);


        }


        return urls;


    }
    /**
     * bilibili api 获取音频号下载地址
     * @param av
     * @param request
     * @return
     */
    public JSONArray appVideo(String av, HttpServletRequest request){
        HashMap<String,String> map = new HashMap<>();
        if(av.startsWith("BV")) {
            map.put("bvid", av);
        }else {
            map.put("aid",av);
        }
        String result = httpClient.get(true,request, "http://api.bilibili.com/x/web-interface/view", map);
        JSONObject videoInfo = JSONObject.parseObject(result).getJSONObject("data");

        String url = "http://api.bilibili.com/x/player/playurl";

        JSONArray urls = new JSONArray();
        int num = (int)videoInfo.get("videos");
        JSONArray pages = videoInfo.getJSONArray("pages");
        for(int i = 0 ; i < num ; i++){
            JSONObject page = pages.getJSONObject(i);
            Map<String,String> map2 = new HashMap<>();
            map2.put("avid",videoInfo.get("aid").toString());
            map2.put("cid",page.get("cid").toString());
            map2.put("qn","120");
            map2.put("fnval","16");
            String s = httpClient.get(true,request, url, map2);
            JSONObject resultVideo = JSONObject.parseObject(s);
            JSONArray video = resultVideo.getJSONObject("data").getJSONObject("dash").getJSONArray("video");
            JSONArray audio = resultVideo.getJSONObject("data").getJSONObject("dash").getJSONArray("audio");
            JSONObject vi = new JSONObject();
            vi.put("video",video);
            vi.put("audio",audio);
            urls.add(vi);


        }


        return urls;


    }

    /**
     * 从av号获取视频/音频信息
     * @param av
     * @param request
     * @return
     */
    public JSONArray getVideo(String av, HttpServletRequest request){
        HashMap<String,String> map = new HashMap<>();
        if(av.startsWith("BV")) {
            map.put("bvid", av);
        }else {
            map.put("aid",av);
        }
        String result = httpClient.get(request, "http://api.bilibili.com/x/web-interface/view", map);
        JSONObject videoInfo = JSONObject.parseObject(result).getJSONObject("data");

        String url = "http://api.bilibili.com/x/player/playurl";

        JSONArray urls = new JSONArray();
        int num = (int)videoInfo.get("videos");
        JSONArray pages = videoInfo.getJSONArray("pages");
        for(int i = 0 ; i < num ; i++){
            JSONObject page = pages.getJSONObject(i);
            Map<String,String> map2 = new HashMap<>();
            map2.put("avid",videoInfo.get("aid").toString());
            map2.put("cid",page.get("cid").toString());
            map2.put("qn","120");
            map2.put("fnval","16");
            String s = httpClient.get(request, url, map2);
            JSONObject resultVideo = JSONObject.parseObject(s);
            JSONArray video = resultVideo.getJSONObject("data").getJSONObject("dash").getJSONArray("video");
            JSONArray audio = resultVideo.getJSONObject("data").getJSONObject("dash").getJSONArray("audio");
            JSONObject vi = new JSONObject();
            vi.put("video",video);
            vi.put("audio",audio);
            urls.add(vi);


        }


        return urls;


    }
    public InputStream getStream(String url,HttpServletRequest request){

        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) new URL(url).openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String agent = request.getHeader("User-Agent");
        System.out.println(agent);
        Cookie[] cookies = request.getCookies();
        List<String> cookieList = new ArrayList<>();
        for (Cookie cookie : cookies) {
            if(cookie.getName().equals("DedeUserID")||
                    cookie.getName().equals("DedeUserID__ckMd5")||
                    cookie.getName().equals("SESSDATA")||
                    cookie.getName().equals("bili_jct")
            )
                cookieList.add(cookie.getName() + "=" + cookie.getValue());
        }
        String cookie = String.join(";", cookieList);
        //String agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
        conn.setRequestProperty("User-Agent", agent);
        conn.setRequestProperty("referer","https://www.bilibili.com");
        conn.setRequestProperty("host","bilibili.com");
        conn.setRequestProperty("cookie",cookie);
        conn.setRequestProperty("method","Get");
        conn.setRequestProperty("Range",request.getHeader("Range"));




        InputStream inputStream = null;
        try {
            inputStream = conn.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return inputStream;

    }

    /**
     * 从url播放视频 返回给respond
     * @param url
     * @param request
     * @param response
     * @return
     */
    public boolean getVideo(String url,HttpServletRequest request,HttpServletResponse response){

        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) new URL(url).openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //String agent = request.getHeader("User-Agent");
        //System.out.println(agent);
        Cookie[] cookies = request.getCookies();
        List<String> cookieList = new ArrayList<>();
        for (Cookie cookie : cookies) {
            if(cookie.getName().equals("DedeUserID")||
                    cookie.getName().equals("DedeUserID__ckMd5")||
                    cookie.getName().equals("SESSDATA")||
                    cookie.getName().equals("bili_jct")
            )
                cookieList.add(cookie.getName() + "=" + cookie.getValue());
        }
        String cookie = String.join(";", cookieList);
        String agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
        conn.setRequestProperty("User-Agent", agent);
        conn.setRequestProperty("referer","https://www.bilibili.com");
        conn.setRequestProperty("host","bilibili.com");
        conn.setRequestProperty("cookie",cookie);
        conn.setRequestProperty("method","Get");

        conn.setRequestProperty("Range", request.getHeader("Range"));

        conn.setReadTimeout(600000);
        conn.setConnectTimeout(600000);
        System.out.println(request.getHeader("Range"));



        InputStream inputStream = null;
        try {
            inputStream = conn.getInputStream();

        } catch (IOException e) {
            e.printStackTrace();
        }

        OutputStream outputStream = null;
        response.reset();
        Map<String, List<String>> headerFields = conn.getHeaderFields();
        for (Map.Entry<String,List<String>> entry : headerFields.entrySet()
        ) {
            response.setHeader(entry.getKey(),String.join(";",entry.getValue()));
        }
        response.setStatus(206);
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Methods","GET");
        response.setHeader("Access-Control-Allow-Headers",":x-requested-with,content-type");
        try {
            outputStream = response.getOutputStream();
            IOUtils.copy(inputStream,outputStream);
            response.flushBuffer();
        } catch (IOException e) {

            e.printStackTrace();
        }


        return true;

    }

    /**
     * 从url播放音频 返回给app
     * @param url
     * @param request
     * @param response
     * @return
     */
    public boolean appAudio(String url,HttpServletRequest request,HttpServletResponse response){

        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) new URL(url).openConnection();

        } catch (IOException e) {
            System.out.println("````````"+url);
            e.printStackTrace();
        }
        //String agent = request.getHeader("User-Agent");
        //System.out.println(agent);
        String cookiesApp = request.getAttribute("cookies").toString();



        String agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
        conn.setRequestProperty("User-Agent", agent);
        conn.setRequestProperty("referer","https://www.bilibili.com");
        conn.setRequestProperty("host","bilibili.com");
        conn.setRequestProperty("cookie",cookiesApp);
        conn.setRequestProperty("method","Get");
        if(request.getHeader("Range") == null || request.getHeader("Range").equals("bytes=0-1048575")){
            conn.setRequestProperty("Range","bytes=0-");
        }else {
            conn.setRequestProperty("Range", request.getHeader("Range"));
        }
        conn.setReadTimeout(600000);
        conn.setConnectTimeout(600000);
        System.out.println(request.getHeader("Range"));
        System.out.println("播放"+url);


        InputStream inputStream = null;
        try {
            inputStream = conn.getInputStream();

        } catch (IOException e) {
            e.printStackTrace();
        }

        OutputStream outputStream = null;
        response.reset();
        Map<String, List<String>> headerFields = conn.getHeaderFields();
        for (Map.Entry<String,List<String>> entry : headerFields.entrySet()
        ) {
            response.setHeader(entry.getKey(),String.join(";",entry.getValue()));
        }
        response.setStatus(206);
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Methods","GET");
        response.setHeader("Access-Control-Allow-Headers",":x-requested-with,content-type");
        try {
            outputStream = response.getOutputStream();
            IOUtils.copy(inputStream,outputStream);
            response.flushBuffer();
        } catch (IOException e) {

            e.printStackTrace();
        }


        return true;

    }



    /**
     * 从url播放音频 返回给respond
     * @param url
     * @param request
     * @param response
     * @return
     */
    public boolean getAudio(String url,HttpServletRequest request,HttpServletResponse response){

        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) new URL(url).openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //String agent = request.getHeader("User-Agent");
        //System.out.println(agent);
        Cookie[] cookies = request.getCookies();
        List<String> cookieList = new ArrayList<>();
        for (Cookie cookie : cookies) {
            if(cookie.getName().equals("DedeUserID")||
                    cookie.getName().equals("DedeUserID__ckMd5")||
                    cookie.getName().equals("SESSDATA")||
                    cookie.getName().equals("bili_jct")
            )
                cookieList.add(cookie.getName() + "=" + cookie.getValue());
        }
        String cookie = String.join(";", cookieList);
        String agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
        conn.setRequestProperty("User-Agent", agent);
        conn.setRequestProperty("referer","https://www.bilibili.com");
        conn.setRequestProperty("host","bilibili.com");
        conn.setRequestProperty("cookie",cookie);
        conn.setRequestProperty("method","Get");
        if(request.getHeader("Range") == null || request.getHeader("Range").equals("bytes=0-1048575")){
            conn.setRequestProperty("Range","bytes=0-");
        }else {
            conn.setRequestProperty("Range", request.getHeader("Range"));
        }
        conn.setReadTimeout(600000);
        conn.setConnectTimeout(600000);
        System.out.println(request.getHeader("Range"));



        InputStream inputStream = null;
        try {
            inputStream = conn.getInputStream();

        } catch (IOException e) {
            e.printStackTrace();
        }

        OutputStream outputStream = null;
        response.reset();
        Map<String, List<String>> headerFields = conn.getHeaderFields();
        for (Map.Entry<String,List<String>> entry : headerFields.entrySet()
        ) {
            response.setHeader(entry.getKey(),String.join(";",entry.getValue()));
        }
        response.setStatus(206);
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Methods","GET");
        response.setHeader("Access-Control-Allow-Headers",":x-requested-with,content-type");
        try {
            outputStream = response.getOutputStream();
            IOUtils.copy(inputStream,outputStream);
            response.flushBuffer();
        } catch (IOException e) {

            e.printStackTrace();
        }


        return true;

    }

    public boolean down(String url,HttpServletRequest request){

        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) new URL(url).openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //String agent = request.getHeader("User-Agent");
        //System.out.println(agent);
        Cookie[] cookies = request.getCookies();
        List<String> cookieList = new ArrayList<>();
        for (Cookie cookie : cookies) {
            if(cookie.getName().equals("DedeUserID")||
                    cookie.getName().equals("DedeUserID__ckMd5")||
                    cookie.getName().equals("SESSDATA")||
                    cookie.getName().equals("bili_jct")
            )
                cookieList.add(cookie.getName() + "=" + cookie.getValue());
        }
        String cookie = String.join(";", cookieList);
        String agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
        conn.setRequestProperty("User-Agent", agent);
        conn.setRequestProperty("referer","https://www.bilibili.com");
        conn.setRequestProperty("host","bilibili.com");
        conn.setRequestProperty("cookie",cookie);
        conn.setRequestProperty("method","Get");
        conn.setRequestProperty("Range","bytes=0-");


        conn.setReadTimeout(600000);
        conn.setConnectTimeout(600000);
        System.out.println(request.getHeader("Range"));



        InputStream inputStream = null;
        try {
            inputStream = conn.getInputStream();

        } catch (IOException e) {
            e.printStackTrace();
        }



        try {
            OutputStream outputStream = new FileOutputStream(new File("D:/aaaaaa.m4s"));
            IOUtils.copy(inputStream,outputStream);
            outputStream.flush();

        } catch (IOException e) {

            e.printStackTrace();
        }


        return true;

    }

    /**
     * 从url获取flv播放
     * @param url
     * @param request
     * @param response
     * @return
     */
    public boolean getFlv(String url,HttpServletRequest request,HttpServletResponse response){

        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) new URL(url).openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //String agent = request.getHeader("User-Agent");
        //System.out.println(agent);
        Cookie[] cookies = request.getCookies();
        List<String> cookieList = new ArrayList<>();
        for (Cookie cookie : cookies) {
            if(cookie.getName().equals("DedeUserID")||
                    cookie.getName().equals("DedeUserID__ckMd5")||
                    cookie.getName().equals("SESSDATA")||
                    cookie.getName().equals("bili_jct")
            )
                cookieList.add(cookie.getName() + "=" + cookie.getValue());
        }
        String cookie = String.join(";", cookieList);
        String agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
        conn.setRequestProperty("User-Agent", agent);
        conn.setRequestProperty("referer","https://www.bilibili.com");
        conn.setRequestProperty("host","bilibili.com");
        conn.setRequestProperty("cookie",cookie);
        conn.setRequestProperty("method","post");
        if(request.getHeader("Range") == null || request.getHeader("Range").equals("bytes=0-1048575")){

        }else {
            conn.setRequestProperty("Range", request.getHeader("Range"));
        }
        conn.setReadTimeout(600000);
        conn.setConnectTimeout(600000);
        System.out.println(request.getHeader("Range"));



        InputStream inputStream = null;
        try {
            inputStream = conn.getInputStream();

        } catch (IOException e) {
            e.printStackTrace();
        }

        OutputStream outputStream = null;
        response.reset();
        Map<String, List<String>> headerFields = conn.getHeaderFields();
        for (Map.Entry<String,List<String>> entry : headerFields.entrySet()
        ) {
            response.setHeader(entry.getKey(),String.join(";",entry.getValue()));
        }
        response.setStatus(206);
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Methods","GET");
        response.setHeader("Access-Control-Allow-Headers",":x-requested-with,content-type");
        try {
            outputStream = response.getOutputStream();
            IOUtils.copy(inputStream,outputStream);
            response.flushBuffer();
        } catch (IOException e) {

            e.printStackTrace();
        }


        return true;

    }

    /**
     * 从url获取flv播放
     * @param url
     * @param request
     * @param response
     * @return
     */
    public boolean getFlv(String url,String cookies,HttpServletRequest request,HttpServletResponse response){

        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) new URL(url).openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //String agent = request.getHeader("User-Agent");
        //System.out.println(agent);


        String agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
        conn.setRequestProperty("User-Agent", agent);
        conn.setRequestProperty("referer","https://www.bilibili.com");
        conn.setRequestProperty("host","bilibili.com");
        conn.setRequestProperty("cookie",cookies);
        conn.setRequestProperty("method","post");
        if(request.getHeader("Range") == null || request.getHeader("Range").equals("bytes=0-1048575")){

        }else {
            conn.setRequestProperty("Range", request.getHeader("Range"));
        }
        conn.setReadTimeout(600000);
        conn.setConnectTimeout(600000);
        System.out.println(request.getHeader("Range"));



        InputStream inputStream = null;
        try {
            inputStream = conn.getInputStream();

        } catch (IOException e) {
            e.printStackTrace();
        }

        OutputStream outputStream = null;
        response.reset();
        Map<String, List<String>> headerFields = conn.getHeaderFields();
        for (Map.Entry<String,List<String>> entry : headerFields.entrySet()
        ) {
            response.setHeader(entry.getKey(),String.join(";",entry.getValue()));
        }
        response.setStatus(206);
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Methods","GET");
        response.setHeader("Access-Control-Allow-Headers",":x-requested-with,content-type");
        try {
            outputStream = response.getOutputStream();
            IOUtils.copy(inputStream,outputStream);
            response.flushBuffer();
        } catch (IOException e) {

            e.printStackTrace();
        }


        return true;

    }



    @Deprecated
    public void getFlvStream(String url, HttpServletRequest request, HttpServletResponse response){

        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) new URL(url).openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String agent = request.getHeader("User-Agent");
        System.out.println(agent);
        Cookie[] cookies = request.getCookies();
        List<String> cookieList = new ArrayList<>();
        for (Cookie cookie : cookies) {
            if(cookie.getName().equals("DedeUserID")||
                    cookie.getName().equals("DedeUserID__ckMd5")||
                    cookie.getName().equals("SESSDATA")||
                    cookie.getName().equals("bili_jct")
            )
                cookieList.add(cookie.getName() + "=" + cookie.getValue());
        }
        String cookie = String.join(";", cookieList);
        //String agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
        conn.setRequestProperty("User-Agent", agent);
        conn.setRequestProperty("referer","https://www.bilibili.com");
        conn.setRequestProperty("host","bilibili.com");
        conn.setRequestProperty("cookie",cookie);
        conn.setRequestProperty("method","Get");
        conn.setRequestProperty("Range",request.getHeader("Range"));




        InputStream inputStream = null;
        try {

            conn.connect();
            inputStream = conn.getInputStream();
            Map<String, List<String>> headerFields = conn.getHeaderFields();
            //response.reset();
//            for (Map.Entry<String, List<String>> m: headerFields.entrySet()){
//
//                response.setHeader(m.getKey(),String.join(";",m.getValue()));
//
//            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Methods","GET");
        response.setHeader("Access-Control-Allow-Headers",":x-requested-with,content-type");
        try {
            response.setStatus(conn.getResponseCode());


            IOUtils.copy(inputStream,response.getOutputStream());


        } catch (IOException e) {
            e.printStackTrace();
        }

    }



}
